package com.pzb.web.controller.goods;

import java.util.List;

import com.pzb.wechat.service.IWechatAppletService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.pzb.common.annotation.Log;
import com.pzb.common.core.controller.BaseController;
import com.pzb.common.core.domain.AjaxResult;
import com.pzb.common.enums.BusinessType;
import com.pzb.goods.domain.GdGoodsUnitsKey;
import com.pzb.goods.service.IGdGoodsUnitsKeyService;

/**
 * 商品规格Controller
 * 
 * @author Mr_Wu
 * @date 2021-12-08
 */
@RestController
@RequestMapping("/goods/unitsKey")
public class GdGoodsUnitsKeyController extends BaseController
{
    @Autowired
    private IGdGoodsUnitsKeyService gdGoodsUnitsKeyService;
    @Autowired
    private IWechatAppletService wechatAppletService;

    /**
     * 查询商品规格列表
     */
    @PreAuthorize("@ss.hasPermi('goods:unitsKey:list')")
    @GetMapping("/list")
    public AjaxResult list(GdGoodsUnitsKey gdGoodsUnitsKey)
    {
        gdGoodsUnitsKey.setExtAppid(wechatAppletService.getExtAppid());
        List<GdGoodsUnitsKey> list = gdGoodsUnitsKeyService.selectGdGoodsUnitsKeyList(gdGoodsUnitsKey);
        return AjaxResult.success(list);
    }

    /**
     * 查询商品规格列表
     */
    @PreAuthorize("@ss.hasPermi('goods:unitsKey:list')")
    @GetMapping("/unitsKeyValueList")
    public AjaxResult unitsKeyValueList(GdGoodsUnitsKey gdGoodsUnitsKey)
    {
        gdGoodsUnitsKey.setExtAppid(wechatAppletService.getExtAppid());
        List<GdGoodsUnitsKey> list = gdGoodsUnitsKeyService.selectGdGoodsUnitsKeyValueList(gdGoodsUnitsKey);
        return AjaxResult.success(list);
    }


    /**
     * 获取商品规格详细信息
     */
    @PreAuthorize("@ss.hasPermi('goods:unitsKey:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(gdGoodsUnitsKeyService.selectGdGoodsUnitsKeyById(id));
    }

    /**
     * 新增商品规格
     */
    @PreAuthorize("@ss.hasPermi('goods:unitsKey:add')")
    @Log(title = "商品规格", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GdGoodsUnitsKey gdGoodsUnitsKey)
    {
        String extAppid = wechatAppletService.getExtAppid();
        if("1".equals(extAppid)){
            return AjaxResult.error("您还没有小程序!");
        }
        gdGoodsUnitsKey.setExtAppid(extAppid);
        return toAjax(gdGoodsUnitsKeyService.insertGdGoodsUnitsKey(gdGoodsUnitsKey));
    }

    /**
     * 修改商品规格
     */
    @PreAuthorize("@ss.hasPermi('goods:unitsKey:edit')")
    @Log(title = "商品规格", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GdGoodsUnitsKey gdGoodsUnitsKey)
    {
        return toAjax(gdGoodsUnitsKeyService.updateGdGoodsUnitsKey(gdGoodsUnitsKey));
    }

    /**
     * 删除商品规格
     */
    @PreAuthorize("@ss.hasPermi('goods:unitsKey:remove')")
    @Log(title = "商品规格", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        if (gdGoodsUnitsKeyService.checkUnitsKeyExistValues(id))
        {
            return AjaxResult.error("规格存在规格值,不允许删除");
        }
        return toAjax(gdGoodsUnitsKeyService.deleteGdGoodsUnitsKeyById(id));
    }
}
